package com.xxxy.controller;

import com.xxxy.commonResult.CommonResult;
import com.xxxy.commonResult.CommonResultPage;
import com.xxxy.log.Log;
import com.xxxy.model.AdministrativeClass;
import com.xxxy.model.Student;
import com.xxxy.model.TeachingClass;
import com.xxxy.model.TeachingClassStudent;
import com.xxxy.response.TeachingClassResponse;
import com.xxxy.service.AdministrativeClassService;
import com.xxxy.service.TeachingClassService;
import com.xxxy.service.TeachingClassStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author sjh
 * @date 2022/2/2 12:18
 */
@RestController
@Api(tags = "教学班级管理")
@RequestMapping("/teachingClass")
public class TeachingClassController {

    @Autowired
    TeachingClassStudentService teachingClassStudentService;

    @Autowired
    TeachingClassService teachingClassService;

    @Autowired
    AdministrativeClassService administrativeClassService;

    @Log(module = "教学班级管理", description = "增加教学班级")
    @GetMapping(value = "/addTeachingClass")
    @ApiOperation(value = "增加教学班级")
    public CommonResult<Boolean> addTeachingClass(@ApiParam("教学班级信息") @RequestBody TeachingClass teachingClass) {
        boolean flag = teachingClassService.save(teachingClass);
        if (flag) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Log(module = "教学班级管理", description = "获取所有教学班级")
    @PostMapping(value = "/list/all")
    @ApiOperation(value = "获取所有教学班级")
    public CommonResult<CommonResultPage> list(@ApiParam("页数") @RequestParam(defaultValue = "1") Integer page,
                                               @ApiParam("一页条数") @RequestParam(defaultValue = "10") Integer size,
                                               @ApiParam("教学班级信息") @RequestBody TeachingClass teachingClass) {
        return CommonResult.success(teachingClassService.list(page, size, teachingClass));
    }

    @Log(module = "教学班级管理", description = "获取所属或教学的班级")
    @GetMapping(value = "/list")
    @ApiOperation(value = "获取所属或教学的班级")
    public CommonResult<Map<String, List<Integer>>> list(@ApiParam("登录账号") @RequestParam String account) {
        return CommonResult.success(teachingClassStudentService.list(account));
    }

    @Log(module = "教学班级管理", description = "查询教学班级内的学生")
    @GetMapping(value = "/teaching/student")
    @ApiOperation(value = "查询教学班级内的学生")
    public CommonResult<CommonResultPage> getStudentByTeachingClass(@ApiParam("页数") @RequestParam(defaultValue = "1") Integer page,
                                                                    @ApiParam("一页条数") @RequestParam(defaultValue = "10") Integer size,
                                                                    @ApiParam("教学班级id") @RequestParam Integer teachingClassId) {
        return CommonResult.success(teachingClassStudentService.getStudentByTeachingClass(page, size, teachingClassId));
    }

    @Log(module = "教学班级管理", description = "增加教学班的学生（单个）")
    @PostMapping(value = "/addStudent")
    @ApiOperation(value = "增加教学班的学生（单个）")
    public CommonResult<Boolean> addStudent(@ApiParam("教学班级与学生对象") @RequestBody TeachingClassStudent teachingClassStudent) {
        boolean flag = teachingClassStudentService.save(teachingClassStudent);
        if (flag) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Log(module = "教学班级管理", description = "删除教学班的学生（单个）")
    @DeleteMapping(value = "/deleteStudent")
    @ApiOperation(value = "删除教学班的学生（单个）")
    public CommonResult<Boolean> deleteStudent(@ApiParam("教学班级id") @RequestParam Integer teachingClassId,
                                               @ApiParam("学生账号") @RequestParam String studentAccount) {
        boolean flag = teachingClassStudentService.deleteStudent(teachingClassId, studentAccount);
        if (flag) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

}
